package com.itheima10;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Xiao
 * @date 2021/7/27
 */
public class StudentTest {
    /**
     * HashMap集合存储自定义对象并遍历
     *      创建一个HashMap集合，键是学生对象（Student），值是籍贯（String）。
     *      存储三个键值对元素并遍历。
     */
    public static void main(String[] args) {
        HashMap<Student, String> hm = new HashMap<>();
        Student stu1 = new Student("aliang01", 24);
        Student stu2 = new Student("aliang02", 22);
        Student stu3 = new Student("aliang03", 23);
        Student stu4 = new Student("aliang04", 21);
        Student stu5 = new Student("aliang05", 25);
        hm.put(stu1, "湖南");
        hm.put(stu2, "湖北");
        hm.put(stu3, "广州");
        hm.put(stu4, "天津");
        hm.put(stu5, "上海");
        Set<Map.Entry<Student, String>> entries = hm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }
        System.out.println("----------");
        for (Student stu : hm.keySet()) {
            System.out.println(stu + "\t" + hm.get(stu));
        }
        System.out.println("----------");
        hm.forEach((Student stu,String s) -> {
            System.out.println(stu + "\t" + s);
        });
    }
}
